package top.lauwen.service;

import android.content.Intent;
import java.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class alarmService extends AppCompatActivity implements View.OnClickListener{
    private static alarmService appRef = null;
    private Button btnOne,btnTwo;
    boolean k = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appRef = this;
        setContentView(R.layout.activity_main);
        btnOne = (Button) findViewById(R.id.buttonOne);
        btnOne.setOnClickListener(this);
        btnTwo = (Button) findViewById(R.id.buttonTwo);
        btnTwo.setOnClickListener(this);
    }

        public static alarmService getApp() {
            return appRef;
        }
        public void btEvent(String data) {
            setTitle(data);
        }
        public void onClick(View arg0) {
            if(arg0 == btnOne) {
                setTitle("Wait for 5 second--->alarm");
                Intent intent = new Intent(alarmService.this,alarmReciver.class);
                PendingIntent p_intent = PendingIntent.getBroadcast(alarmService.this,0,intent,0);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.SECOND,3);

                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),p_intent);
            }

            if(arg0 == btnTwo) {
                Intent intent = new Intent(alarmService.this,alarmReciver.class);
                PendingIntent p_intent = PendingIntent.getBroadcast(alarmService.this,0,intent,0);

                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.cancel(p_intent);
                finish();
            }

        }

}
